package cn.com.utt.unms.controller;

import java.security.Principal;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.com.utt.common.util.LocaleMessageSourceUtil;
import cn.com.utt.common.util.Tips;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.TableInput;
import cn.com.utt.common.vo.TableOutput;
import cn.com.utt.common.vo.User;
import cn.com.utt.unms.service.AccountService;
import cn.com.utt.unms.vo.AccountInfo;

@RestController
@RequestMapping("v1/accounts")
public class AccountController {

	@Autowired
	private AccountService accountService;
	
	@Autowired
    private LocaleMessageSourceUtil messageSourceUtil;

	@PreAuthorize("hasRole('ACCOUNT_EDIT')")
	@RequestMapping(value = "/create", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> save(@RequestBody AccountInfo accountInfo, Principal principal) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		accountInfo.setParentId(user.getId());
		result = accountService.save(accountInfo, user.getId());
		return result.toJsonObject();
	}

	@PreAuthorize("hasRole('ACCOUNT_EDIT')")
	@RequestMapping(value = "/update", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> update(@RequestBody AccountInfo accountInfo, Principal principal) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		result = accountService.update(accountInfo, user);
		return result.toJsonObject();
	}

	@PreAuthorize("hasRole('ACCOUNT_EDIT')")
	@RequestMapping(value = "/delete", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> delete(@RequestParam(value = "userIds[]") List<Long> userIds, Principal principal) {
		RestResult result = new RestResult();
		if (userIds.size() < 0) {
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		result = accountService.delete(userIds);
		return result.toJsonObject();
	}

	@PreAuthorize("hasRole('ACCOUNT_VIEW')")
	@RequestMapping(value = "/list", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public Map<String, Object> list(@RequestBody TableInput input, Principal principal) {
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		TableOutput<AccountInfo> output = accountService.queryList(input, user);
		result.put("start", output.getStart());
		result.put("rows", output.getRows());
		result.put("total", output.getTotal());
		result.put("accounts", output.getList());
		return result.toJsonObject();
	}

}
